package com.atheima.reggie.controller;

import com.atheima.reggie.common.R;
import com.atheima.reggie.entity.User;
import com.atheima.reggie.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody Map param, HttpServletRequest request) {

        //6.解析结果集

        //resultSet.next()返回的是一个布尔值
        //第一层循环返回的是第一行




        String phone = param.get("phone").toString();
        String code = param.get("code").toString();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        User user = userService.getOne(wrapper);
        if(user == null) {
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);

        }
        request.getSession().setAttribute("userId",user.getId());
        return R.ok();


    }
}
